<template>
	<view>
		<view class="setup_box">
			<view v-if="userinfo.is_verify != 1" class="setup_list" @click="change_tel">
				<text>修改手机号</text>
				<view class="setup_edition">
					<text>{{userinfo.mobile}}</text>
					<u-icon name="arrow-right" color="#999999" size="18"></u-icon>
				</view>
			</view>
			<view class="setup_list" @click="change_pwd">
				<text>修改密码</text>
				<u-icon name="arrow-right" color="#999999" size="18"></u-icon>
			</view>
			<view class="setup_list" @click="set_pay_pwd">
				<text>设置支付密码</text>
				<u-icon name="arrow-right" color="#999999" size="18"></u-icon>
			</view>
			<view class="setup_list" @click="about_us(3,'用户协议')">
				<text>用户协议</text>
				<u-icon name="arrow-right" color="#999999" size="18"></u-icon>
			</view>
			<!-- #ifdef APP-PLUS -->
			<view class="setup_list" @click="clearcache">
				<text>清理缓存</text>
				<view class="setup_edition">
					<text>{{currentSize}}</text>
					<u-icon name="arrow-right" color="#999999" size="18"></u-icon>
				</view>
			</view>
			<!-- #endif -->
			<view class="setup_list">
				<text>查看更新</text>
				<view class="setup_edition">
					<text>当前版本{{app_version}}</text>
					<u-icon name="arrow-right" color="#999999" size="18"></u-icon>
				</view>
			</view>
			<view class="Logout" @click="order">退　出</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				app_version: '1.0',
				currentSize: '',
				userinfo: {},
				isCheckPayPass: 0
			}
		},
		onLoad() {
			this.userinfo = uni.getStorageSync('userinfo')
			var that = this
			/*#ifdef APP-PLUS*/
			plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
				that.app_version = wgtinfo.version
			})
			this.getStorageSize()
			/*#endif*/
			/*#ifdef H5*/
			this.loadVersion()
			/*#endif*/
			this.checkPayPass()
		},
		methods: {
			// 修改手机号
			change_tel() {
				uni.navigateTo({
					url: '/pages/version5/mine/setup/changephone/changephone'
				})
			},
			// 修改密码
			change_pwd() {
				uni.navigateTo({
					url: '/pages/version5/mine/setup/ChangePassword/ChangePassword'
				})
			},
			// 设置支付密码
			set_pay_pwd() {
				if (this.isCheckPayPass == 1) {
					uni.navigateTo({
						url: '/pages/version5/mine/setup/ChangePassword/editPayPassword'
					})
				} else {
					uni.navigateTo({
						url: '/pages/version5/mine/setup/ChangePassword/setPayPassword'
					})
				}
			},
			// 检测是否设置支付密码
			async checkPayPass() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Secret/check_pay_pass',
					header: {
						'token': this.userinfo.token
					}
				});
				if (res.code == 1) {
					this.isCheckPayPass = res.data
				}
			},
			// 提示退出
			order() {
				let _this = this
				uni.showModal({
					title: '提示',
					content: '是否退出登录',
					success: function(res) {
						if (res.confirm) {
							_this._Logout()
						} else if (res.cancel) {}
					}
				});
			},
			// 获取APP版本
			async loadVersion() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Setting/getAppVersion',
					data: {},
					header: {
						'token': this.userinfo.token
					},
				});
				if (res.code == 1) {
					if (res.data != '' && res.data != null) {
						this.app_version = res.data.version_number
					}
				}
			},
			// 退出
			async _Logout() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp5({
					url: 'user/logout',
					data: {},
					header: {
						'token': this.userinfo.token
					},
				});
				uni.showToast({
					title: res.msg,
					duration: 2000,
					icon: 'none',
					success() {
						uni.removeStorageSync('userinfo');
						uni.reLaunch({
							url: '/pages/login/login'
						})
					}
				});
			},
			// 协议
			about_us(id, name) {
				uni.navigateTo({
					url: '/pages/version5/mine/aboutus/aboutus?id=' + id + '&name=' + name
				})
			},
			// 获取本地缓存大小
			getStorageSize() {
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.currentSize = "0B";
					} else if (sizeCache < 1024) {
						that.currentSize = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						that.currentSize = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						that.currentSize = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						that.currentSize = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
				});
			},
			// 清理缓存
			clearcache() {
				let that = this;
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录
									that.files = []
									uni.showToast({
										title: '清除成功',
										duration: 2000
									});
									that.getStorageSize(); // 重新计算缓存  
								}, function(e) {
								});
							} else {
								entry.remove();
							}
						}, function(e) {
						});
					}
				} else { // ios  
					plus.cache.clear(function() {
						uni.showToast({
							title: '清除成功',
							duration: 2000
						});
						that.getStorageSize();
					});
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.setup_box {
		.setup_list {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 40rpx 30rpx;
			box-sizing: border-box;
			border-bottom: #dedede 1px solid;

			text {
				font-size: 28rpx;
			}

			.setup_edition {
				display: flex;
				justify-content: flex-end;

				text {
					color: #999999;
				}
			}
		}

		.Logout {
			background-color: #1296db;
			margin: 50rpx 20rpx 0 20rpx;
			text-align: center;
			padding: 30rpx 0;
			border-radius: 15rpx;
			color: #fff;
			transition: all 0.5s;
		}

		.Logout:active {
			background-color: #1296db;
		}
	}
</style>